import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_base/base/base_container.dart';
import 'package:flutter_base/net/utils/BaseNetWork.dart';
import 'package:flutter_base/resource/theme/dark_mode_provider.dart';
import 'package:flutter_base/resource/theme/theme_color.dart';
import 'package:image_picker/image_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

class ImgPickerPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MyHomePage();
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  File _image;
  final picker = ImagePicker();

  Future getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);

    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);
      } else {
        print('No image selected.');
      }
    });
  }

  bool showLoading = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _load();
  }

  void _load() {
    setState(() {
      showLoading = true;
    });
    Future.delayed(Duration(seconds: 3), () {
      setState(() {
        showLoading = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<DarkModeProvider>(builder: (context, appInfo, _) {
      return BaseContainer(
        showToolBar: true,
        toolbarHeight: 50,
        titleColor: Colors.white,
        statusBgColor: Colors.blue,
        titleSize: 20.sp,
        decoration: BoxDecoration(color: g1),
        icon: Icon(
          Icons.arrow_back_rounded,
          color: Colors.white,
        ),
        toolbarBgColor: Colors.blue,
        title: 'Image Picker Example',
        child: GestureDetector(
          onTap: () {
            _load();
          },
          child: Center(
            child: _image == null
                ? Text(
                    'No image sele2cted.',
                    style: TextStyle(color: t1),
                  )
                : Image.file(_image),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: getImage,
          tooltip: 'Pick Image',
          child: Icon(Icons.add_a_photo),
        ),
        statusDarkColor: true,
      );
    });
  }
}
